//
//  NewsItemViewController.m
//  ShowYork
//
//  Created by hxh on 2021/1/18.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "NewsItemViewController.h"
#import "CommonWebViewController.h"
#import "NewsListItemCell.h"
#import "NewsListItemModel.h"
@interface NewsItemViewController ()<UITableViewDelegate,UITableViewDataSource,NewsListItemCellDelegate>
@property (nonatomic,copy)void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NewsTagItemModel *itemModel;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *dataArray;



@end

@implementation NewsItemViewController


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (instancetype)initWithNewsTagItemModel:(NewsTagItemModel *)itemModel{
    if (self = [super init]) {
        self.itemModel = itemModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    [self loadDataSource];
    
    // Do any additional setup after loading the view.
}

- (void)setUI{
    
    self.isHideNavBar = YES;
    
    self.page = 1;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    

    __weak typeof(self)weakSelf = self;
    
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page++;
        [weakSelf loadDataSource];
    }];
    
    [footer setTitle:@"—— 没有更多了 ——" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = font(10);
    footer.stateLabel.textColor = RGBA(153, 153, 153, 1);
    footer.hidden = YES;
    self.tableView.mj_footer = footer;

    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf loadDataSource];
    }];
    self.tableView.mj_header = header;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.tableView.frame = self.view.bounds;
}

- (void)loadDataSource{
    
    
    [self.tableView.mj_header endRefreshing];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"md5"] = [RPAppManager md5StrWithApi:GetNewsList];
    param[@"tagId"] = self.itemModel.id;
    param[@"pageIndex"] = @(self.page);
    param[@"pageSize"] = @(10);
    [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(GetNewsList) parames:param success:^(id responseObj) {
        if (self.page == 1) {
            self.dataArray = [NewsListItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]];
        }else{
            [self.dataArray addObjectsFromArray:[NewsListItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]]];
        }
        [self stopRefresh];
        
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        CGFloat bottom = self.tableView.mj_insetB;
        if ([responseObj[RESPONSE][LASTPAGE] boolValue] || ![responseObj[RESPONSE][NEXTPAGE]boolValue]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            if (self.tableView.mj_footer.cl_height == 44.0f) {
                self.tableView.mj_footer.cl_height = 138.0f;
                if (bottom == 44.0f || bottom == GK_SAVEAREA_BTM + 44.0f) {
                    self.tableView.mj_insetB = 138.0f;
                }
            }
        }else{
            [self.tableView.mj_footer resetNoMoreData];
            if (self.tableView.mj_footer.cl_height == 138.0f) {
                self.tableView.mj_footer.cl_height = 44.0f;
                if (bottom == 138.0f) {
                    self.tableView.mj_insetB = 44.0f;
                }
            }
        }
        [self.tableView reloadData];
        
    } failure:^(id error) {
        [self stopRefresh];
    }];
}

- (void)stopRefresh{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsListItemCell *cell = [NewsListItemCell cellWithTableView:tableView];
    cell.delegate = self;
    [cell setItemModel:self.dataArray[indexPath.item]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (void)showNewsDetailByCurrentModel:(NewsListItemModel *)itemModel{
    CommonWebViewController *dtlVC = [[CommonWebViewController alloc]init];
    dtlVC.webUrl = [NSString stringWithFormat:@"%@lesson/news/%@?isApp=true&userId=%@",HtmlUrl,itemModel.id,[RPAppManager getCustomerId]];
    dtlVC.isDark = NO;
    [self.navigationController pushViewController:dtlVC animated:YES];
}

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.tableView;
}


- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
   
}

@end
